# Copyright (C) 1998,1999 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 

# Please email any bugs, comments, and/or additions to this file to:
# bug-gcc@prep.ai.mit.edu

# This file was created for by Vladimir Makarov (vmakarov@cygnus.com)
# on the base c-torture.exp.

# The script requires setting up BASE_COMPILER (e.g. in RUNTESTFLAGS).
# The default options can be overridden by
# CONSISTENCY_OPTIONS="option ... " (e.g. in RUNTESTFLAGS).


if $tracelevel then {
    strace $tracelevel
}

if ![info exists BASE_COMPILER] {
    fail "BASE_COMPILER is not set"  
    return
}

if ![info exists CONSISTENCY_OPTIONS] {
    set CONSISTENCY_OPTIONS "-O2"
}

if ![info exists NO_LONG_DOUBLE] {
    set NO_LONG_DOUBLE 0
}

if ![info exists NO_LIBGLOSS_LIBRARY_OPTIONS] {
    set NO_LIBGLOSS_LIBRARY_OPTIONS 0
}

puts $CONSISTENCY_OPTIONS

regsub -- -.* $target_triplet "" target_chip

#
# c-consistency-execute -- utility to compile and execute a testcase
#
# SRC is the full pathname of the testcase.
#
# If the testcase has an associated .cexp file, we source that to run the
# test instead.  We use .cexp instead of .exp so that the testcase is still
# controlled by the main .exp driver (this is useful when one wants to only
# run the compile.exp tests for example - one need only pass compile.exp to
# dejagnu, and not compile.exp, foo1.exp, foo2.exp, etc.).
#
proc c-consistency-execute { src cpp_flag out_suffix} {
    global tmpdir tool srcdir output
    global exec_output target_chip BASE_COMPILER target_triplet
    global CONSISTENCY_OPTIONS NO_LONG_DOUBLE

    set consistency_options $CONSISTENCY_OPTIONS

    set rootname [file rootname $src]
    set dirname [file dirname $src]
    set basename [file tail $rootname]
    # Check for alternate driver.
    if [file exists $rootname.cexp] {
	verbose "Using alternate driver $basename.cexp" 2
	set done_p 0
	catch "set done_p \[source $rootname.cexp\]"
	if { $done_p } {
	    return
	}
    }
   
    set executable $tmpdir/$basename.x

    regsub "^$srcdir/?" $src "" testcase
    # If we couldn't rip $srcdir out of `src' then just do the best we can.
    # The point is to reduce the unnecessary noise in the logs.  Don't strip
    # out too much because different testcases with the same name can confuse
    # `test-tool'.
    if [string match "/*" $testcase] {
	set testcase "[file tail $dirname]/[file tail $src]"
    }

    # consistency_{compile,execute}_xfail are set by the .cexp script
    # (if present)
    if [info exists consistency_compile_xfail] {
        setup_xfail $consistency_compile_xfail
    }
    remote_file build delete $executable
    verbose "Testing $testcase, $consistency_options" 1
    
    set options ""
    lappend options "additional_flags=-w $consistency_options"

    set wrap_file ""
    if {[file exists "$dirname/WRAP_FILE"] \
         && [file readable "$dirname/WRAP_FILE"]} {
      set fd [open "$dirname/WRAP_FILE"]
      set wrap_file "$dirname/[read -nonewline $fd]"
      close $fd
    }

    set comp_output [gcc_target_compile "$cpp_flag $src -x none $wrap_file" "$executable" executable $options];
    
    # Set a few common compiler messages.
    set fatal_signal "*cc: Internal compiler error: program*got fatal signal"
    
    if [string match "$fatal_signal 6" $comp_output] then {
        fail $testcase "Got Signal 6, $cpp_flag $consistency_options"
        remote_file build delete $executable
        return
    }
    
    if [string match "$fatal_signal 11" $comp_output] then {
        fail $testcase "Got Signal 11, $cpp_flag $consistency_options"
        remote_file build delete $executable
        return
    }
    
    # We shouldn't get these because of -w, but just in case.
    if [string match "*cc:*warning:*" $comp_output] then {
        warning "$testcase: (with warnings) $cpp_flag $consistency_options"
        send_log "$comp_output\n"
        unresolved "$testcase, $cpp_flag $consistency_options"
        remote_file build delete $executable
        return
    }
    
    set comp_output [prune_warnings $comp_output]
    
    if { ![file exists $executable] } {
        if ![is3way] {
   	    	fail "$testcase compilation, $cpp_flag $consistency_options"
    		untested "$testcase execution, $cpp_flag $consistency_options"
    		return
        } else {
    		# FIXME: since we can't test for the existance of a remote
    		# file without short of doing an remote file list, we assume
    		# that since we got no output, it must have compiled.
    		pass "$testcase compilation, $cpp_flag $consistency_options"
        }
    } else {
        pass "$testcase compilation, $cpp_flag $consistency_options"
    }
    
    # Check for compilation only.
    if [file exists $rootname.conly] {
      remote_file build delete $executable
      return;
    }

    # See if this source file uses "long long" types, if it does, and
    # no_long_long is set, skip execution of the test.
    if [target_info exists no_long_long] then {
      if [expr [search_for $src "long long"]] then {
	untested "$testcase execution, $options"
	continue
      }
    }

    if [info exists consistency_execute_xfail] {
        setup_xfail $consistency_execute_xfail
    }
    
    set exec_output ""
    set result [consistency_load "$executable" "" ""]
    set status [lindex $result 0];
    set exec_output [lindex $result 1];

    # Strip random whitespace junk from the output.  the
    # whitejunk is an artifact of the way we get output
    # from boards.
    regsub "^\[ \t\r]+" $exec_output "" clean_output
    regsub -all "\r" $clean_output "" clean_output
    regsub -all "\[ \t]*\n\[ \t]*" $clean_output "\n" clean_output
    regsub -all "\n+" $clean_output "\n" clean_output
    regsub -all "^\n+$" $clean_output "" clean_output

    if { $status == "pass" } {
        remote_file build delete $executable
    }
    $status "$testcase execution, $cpp_flag $consistency_options"

    if { $status != "pass" } {
      return
    }

    # Now save the output of the test programs
    set outfile_name "$tmpdir/$basename.$out_suffix"
    set outf [open $outfile_name "w"]
    if { $clean_output != "" } {
      puts -nonewline $outf $clean_output
    }
    close $outf

    set base_outfile_name "$dirname/${BASE_COMPILER}-results/$basename.$out_suffix"

    # Check that the output for another processor has been saved
    if { ![file exists $base_outfile_name] } {
      untested "$testcase consistency - $base_outfile_name doesn't exist"
      return
    } elseif { ![file readable $base_outfile_name] } {
      untested "$testcase consistency - $base_outfile_name is not readable"
      return
    }

    # See if this source file uses "long double" types, if it does, and
    # NO_LONG_DOUBLE is TRUE, skip comparison of the test.
    if {$NO_LONG_DOUBLE} then {
        if [expr [search_for $src "long double"]] then {
	      setup_xfail "$target_triplet"
        }
    }

    # Compare the outputs
    if {![catch "exec diff -b $outfile_name $base_outfile_name" message]} {
      pass "$testcase consistency, $cpp_flag $consistency_options"
    } else {
      regsub "\n\[^>\n]\[^\n]*\$" $message "\n" difference
      fail "$testcase consistency, $cpp_flag $consistency_options\n$difference"
    }
}

#
# search_for -- looks for a string match in a file
#
proc search_for { file pattern } {
    set fd [open $file r]
    while { [gets $fd cur_line]>=0 } {
	if [string match "*$pattern*" $cur_line] then {
	    close $fd
	    return 1
	}
    }
    close $fd
    return 0
}

if {$NO_LIBGLOSS_LIBRARY_OPTIONS} {
    # Remove standard libgloss library paths
    set board_info(powerpc-sim,ldflags) "-msim"
}

#
# main test loop
#

foreach dir [lsort [glob -nocomplain $srcdir/$subdir/\[a-z\]*]] {
     if [file isdirectory $dir] {
          set all_sources [concat [glob -nocomplain $dir/*.c] [glob -nocomplain $dir/*.cpp]]
          foreach src [lsort $all_sources] {
             # If we're only testing specific files and this isn't one of
             # them, skip it.
             if ![runtest_file_p $runtests $src] then {
	       continue
             }
               regsub "^.*\\." $src "" suffix            
             if {$suffix != "cpp"} {
               c-consistency-execute $src "" "out"
             }
             c-consistency-execute $src "-x c++" "out++"
          }
     }
}
